﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AES小工具
{
    public partial class FrmDataTran : Form
    {

        const int HEX_TO_ASCII = 1;
        const int HEX_TO_GB2312 = 2;
        const int HEX_TO_UTF8 = 3;
        const int HEX_TO_UNICODE = 4;
        const int HEX_TO_BASE64 = 5;

        const int ASCII_TO_HEX = 11;
        const int GB2312_TO_HEX = 12;
        const int UTF8_TO_HEX = 13;
        const int UNICODE_TO_HEX = 14;
        const int BASE64_TO_HEX = 15;

        public FrmDataTran()
        {
            InitializeComponent();
        }

        private void hex2Xxx(int mode)
        {
            string hexStr = tbHex.Text.ToUpper();
            tbAscii.Text = "";
            string[] strs = null;
            int i = 0;
            if (cbHexHasSpilt.Checked) {
                hexStr = tbHex.Text.Trim();
                if ((hexStr.Length % 2) > 0) {
                    MessageBox.Show("输入的数据不正确，请检查!");
                    return;
                }
                int size = hexStr.Length / 2;
                strs = new string[size];
                for (i = 0; i < size; i++) {
                    strs[i] = hexStr.Substring(i * 2, 2);
                }
            } else {
                if (!hexStr.Contains(" ")) {
                    MessageBox.Show("输入的数据请以空格符间隔开!");
                    return;
                }
                strs = hexStr.Split(new char[] { ' ' });
            }
            string asciiStr = "";
            byte[] bytes = new byte[strs.Length];
            i = 0;
            foreach (string str in strs)
            {
                Console.WriteLine(str.Length);
                if (str.Length != 1 && str.Length != 2)
                {
                    MessageBox.Show("请输入 aa bb cc 的格式");
                    return;
                }
                bytes[i++] = Convert.ToByte(str, 16);
            }
            try {
                switch (mode)
                {
                    case HEX_TO_GB2312: { asciiStr = Encoding.GetEncoding("GB2312").GetString(bytes); } break;
                    case HEX_TO_UTF8: { asciiStr = Encoding.GetEncoding("UTF8").GetString(bytes); } break;
                    case HEX_TO_UNICODE: { asciiStr = Encoding.GetEncoding("UNICODE").GetString(bytes); } break;
                    case HEX_TO_BASE64: { asciiStr = Convert.ToBase64String(bytes); } break;
                    case HEX_TO_ASCII:
                    default: { asciiStr = Encoding.ASCII.GetString(bytes); } break;
                }
            } catch (Exception e) { 
                MessageBox.Show(e.Message);
                return;
            }
            
            tbAscii.Text = asciiStr;
        }

        private void xxx2Hex(int mode)
        {
            String asciiStr = tbAscii.Text;
            tbHex.Text = "";
            byte[] bytes = null;
            try {
                switch (mode)
                {
                    case GB2312_TO_HEX: { bytes = Encoding.GetEncoding("GB2312").GetBytes(asciiStr); } break;
                    case UTF8_TO_HEX: { bytes = Encoding.GetEncoding("UTF8").GetBytes(asciiStr); } break;
                    case UNICODE_TO_HEX: { bytes = Encoding.GetEncoding("UNICODE").GetBytes(asciiStr); } break;
                    case BASE64_TO_HEX: { bytes = Convert.FromBase64String(asciiStr); }break;
                    case ASCII_TO_HEX:
                    default: { bytes = Encoding.ASCII.GetBytes(asciiStr); } break;
                }
            } catch(Exception e) {
                MessageBox.Show(e.Message);
                return;
            }
            
            
            string hexStr = "";
            foreach (byte b in bytes) {
                if (!hexStr.Equals("")) {
                    if (cbAddSplit.Checked)
                        hexStr += ",";//勾选以 逗号  分隔
                    else
                        hexStr += " ";//不勾以空格分隔
                }
                if(cbAddOx.Checked) { hexStr += "0x";}
                hexStr += b.ToString("X2");
            }
            tbHex.Text = hexStr;
        }

        private void btnHex2Ascii_Click(object sender, EventArgs e)
        {
            hex2Xxx(HEX_TO_ASCII);
        }

        

        private void btnAscii2Hex_Click(object sender, EventArgs e)
        {
            xxx2Hex(ASCII_TO_HEX);
        }

        private void btnHex2Gb2312_Click(object sender, EventArgs e)
        {
            hex2Xxx(HEX_TO_GB2312);
        }

        private void btnHex2Utf8_Click(object sender, EventArgs e)
        {
            hex2Xxx(HEX_TO_UTF8);
        }

        private void btnHex2Unicode_Click(object sender, EventArgs e)
        {
            hex2Xxx(HEX_TO_UNICODE);
        }

        private void btnGb23122Hex_Click(object sender, EventArgs e)
        {
            xxx2Hex(GB2312_TO_HEX);
        }

        private void btnUtf82Hex_Click(object sender, EventArgs e)
        {
            xxx2Hex(UTF8_TO_HEX);
        }

        private void btnUnicode2Hex_Click(object sender, EventArgs e)
        {
            xxx2Hex(UNICODE_TO_HEX);
        }

        private void btnHex2Base64_Click(object sender, EventArgs e)
        {
            hex2Xxx(HEX_TO_BASE64);
        }

        private void btnBase642Hex_Click(object sender, EventArgs e)
        {
            xxx2Hex(BASE64_TO_HEX);
        }

        private void btnAscii2Base64_Click(object sender, EventArgs e)
        {
            string asciiStr = tbAscii.Text.Trim();
            if (cbLF.Checked) {
                asciiStr += "\r\n";
            }
            byte[] bytes = Encoding.ASCII.GetBytes(asciiStr);
            tbAscii.Text = Convert.ToBase64String(bytes);   
        }

        private void btnBase642Ascii_Click(object sender, EventArgs e)
        {
            byte[] c = Convert.FromBase64String(tbAscii.Text.Trim());
            string asciiStr = System.Text.Encoding.Default.GetString(c);
            if(cbLF.Checked){
                asciiStr = asciiStr.Split('\r')[0];
            }
            tbAscii.Text = asciiStr;
        }
    }
}
